﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sand_game_core
{
    public class Lava : particle
    {
        public Lava(uint x, uint y, int startLight)
            : base(new Microsoft.Xna.Framework.Color(180 + random.rnd.Next(75), 100 + random.rnd.Next(60), 50 + random.rnd.Next(60)), startLight)
        {
            base.x = x;
            base.y = y;
        }
        public override void update()
        {
                upParticle = world.arrayheight > y + 1 ? world.worldParticleLayer[x * world.arraywidth + y + 1] : null;

                downParticle = y >= 1 ? world.worldParticleLayer[x * world.arraywidth + y - 1] : null;

            ammount = (uint)random.rnd.Next(2);
            if (ammount > 0)
            {
            if (downParticle == null || downParticle is Ash)
            {
                world.swap(x, y, x, y - 1);
            }
                    RightParticle = x >= ammount ? world.worldParticleLayer[(x - ammount) * world.arraywidth + y] : null;
                    LeftParticle = world.arraywidth > y + ammount ? world.worldParticleLayer[(x + ammount) * world.arraywidth + y] : null;
                if ((RightParticle is Sand || RightParticle == null) && !(LeftParticle is Sand || LeftParticle == null))
                {
                    world.swap(x, y, x - ammount, y);
                }
                else if ((LeftParticle is Sand || LeftParticle == null) && !(RightParticle is Sand || RightParticle == null))
                {
                    world.swap(x, y, x + ammount, y);
                }
                else
                {
                    if (random.rnd.Next(2) == 0)
                    {
                        world.swap(x, y, x + ammount, y);
                    }
                    else
                    {
                        world.swap(x, y, x - ammount, y);
                    }
                }
            }

            else if (downParticle is Sand)
            {
                downParticle.toTransition = new Lava(downParticle.x, downParticle.y, light);
            }
            else if (LeftParticle is Water)
            {
                LeftParticle.toTransition = new Obsidian(LeftParticle.x, LeftParticle.y, light);
                this.toTransition = new Obsidian(x, y, light);
            }
            else if (RightParticle is Water)
            {
                RightParticle.toTransition = new Obsidian(RightParticle.x, RightParticle.y, light);
                this.toTransition = new Obsidian(x, y, light);
            }
            else if (downParticle is Water)
            {
                downParticle.toTransition = new Obsidian(downParticle.x, downParticle.y, light);
                this.toTransition = new Obsidian(x, y, light);
            }
            else if (LeftParticle is Sand)
            {
                LeftParticle.toTransition = new Lava(LeftParticle.x, LeftParticle.y, light);
            }
            else if (RightParticle is Sand)
            {
                RightParticle.toTransition = new Lava(RightParticle.x, RightParticle.y, light);
            }
            else if (upParticle is Sand)
            {
                upParticle.toTransition =  new Lava(upParticle.x, upParticle.y, light);
            }
            else
            {
                toTransition = upParticle == null ? (random.rnd.Next(75) == 0 ? new Fire(x, y, light) : null) : null; 
                active = toTransition == null ? false : true;
            }
        }

        public override void lightMe()
        {   
                light += light < 254 ? 1 : 0;
        }

        public override void reactivate()
        {
            active = (x + 1 < world.arraywidth && y + 1 < world.arrayheight && x - 1 < world.arraywidth && y - 1 < world.arrayheight && (world.worldParticleLayer[(x + 1) * world.arraywidth + y] == null || world.worldParticleLayer[(x - 1) * world.arraywidth + y] == null || world.worldParticleLayer[x * world.arraywidth + y + 1] == null || world.worldParticleLayer[x * world.arraywidth + y - 1] == null)) ? true : false;
        }
    }
}